/*************************************************************************
	> File Name: hzoj_289.cpp
	> Author: zhangxu
	> Mail: 
	> Created Time: 五  8/23 13:01:37 2024
 ************************************************************************/

#include <cstdio>
#include <unordered_set>
#include <utility>
#include <set>
using namespace std;
// #include <list>
// #include <vector>
#define MAX_N 100000
long long arr[MAX_N + 5];
int st[MAX_N + 5] = {0}, l[MAX_N + 5], r[MAX_N + 5];

typedef pair<int, int> PII;
set<PII> ss;
unordered_set<int> num;
void del(int x) {
	r[l[x]] = r[x];
	l[r[x]] = l[x];
	st[x] = 1;
	return ;
}

int main() {
	int n, m, k = 1;
	scanf("%d%d", &n, &m);
	scanf("%lld", arr + 1);
	for(int i = 1, a; i < n; i++) {
		scanf("%d", &a);
		if(1LL * arr[k] * a < 0) arr[++k] = a;
		else arr[k] += a;
	}
	int cnt = 0;
	long long S = 0;
	for(int i = 1; i <= k; i++) {
		if(arr[i] > 0) {
			cnt += 1;
			S += arr[i];
		}
		ss.insert(PII(abs(arr[i]), i));
		l[i] = i - 1;
		r[i] = i + 1;
	}
	while(cnt > m) {
		while(st[ss.begin()->second]) ss.erase(ss.begin());
		int ind = ss.begin()->second;
		ss.erase(ss.begin());
		int left = l[ind], right = r[ind];
		if((left > 0 && right <= k) || arr[ind] > 0) {
			S -= abs(arr[ind]);
			cnt -= 1;
			arr[ind] += arr[left] + arr[right];
			ss.insert(PII(abs(arr[ind]), ind));
			del(left);
			del(right);
		}
	}
	printf("%lld\n", S);
	return 0;
}
